﻿using System;
using Microsoft.Extensions.DependencyInjection;
//using AyuBookmark.Common.Services;
using AyuBookmark.Common.Repositories;
//using AyuBookmark.WinForm.Forms;
//using AyuBookmark.WinForm.Services;
using AyuBookmark.WinForm.ViewModels;
using AyuBookmark.Common.Interfaces;
using AyuBookmark.WinForm.Dialogs;
using Microsoft.Extensions.Logging;

namespace AyuBookmark.WinForm
{
    /// <summary>
    /// 依赖注入配置根容器
    /// </summary>
    public static class CompositionRoot
    {
        private static IServiceProvider _serviceProvider;

        /// <summary>
        /// 初始化依赖注入容器
        /// </summary>
        public static void Initialize()
        {
            var services = new ServiceCollection();

            // 配置日志
            ConfigureLogging(services);

            // 注册服务
            ConfigureServices(services);

            // 注册仓储
            ConfigureRepositories(services);

            // 注册视图模型
            ConfigureViewModels(services);

            // 注册窗体
            ConfigureForms(services);

            _serviceProvider = services.BuildServiceProvider();
        }

        /// <summary>
        /// 从容器解析服务实例
        /// </summary>
        public static T GetService<T>() where T : class
        {
            return _serviceProvider.GetService<T>();
        }

        private static void ConfigureLogging(IServiceCollection services)
        {
            services.AddLogging(configure =>
            {
                configure.AddDebug();
                configure.AddConsole();
                configure.SetMinimumLevel(LogLevel.Information);
            });
        }

        private static void ConfigureServices(IServiceCollection services)
        {
            //// 应用服务
            //services.AddSingleton<IBookmarkService, BookmarkService>();
            //services.AddSingleton<ICategoryService, CategoryService>();
            //services.AddSingleton<IImportExportService, ImportExportService>();

            //// 工具服务
            //services.AddSingleton<IDialogService, DialogService>();
            //services.AddSingleton<IFileService, FileService>();
        }

        private static void ConfigureRepositories(IServiceCollection services)
        {
            //services.AddSingleton<IBookmarkRepository, BookmarkJsonRepository>();
            //services.AddSingleton<ICategoryRepository, CategoryJsonRepository>();
            //services.AddSingleton<IConfigRepository, AppConfigRepository>();
        }

        private static void ConfigureViewModels(IServiceCollection services)
        {
            services.AddTransient<MainViewModel>();
            //services.AddTransient<BookmarkListViewModel>();
            //services.AddTransient<CategoryTreeViewModel>();
            //services.AddTransient<ImportExportViewModel>();
        }

        private static void ConfigureForms(IServiceCollection services)
        {
            services.AddSingleton<MainForm>();
            //services.AddTransient<BookmarkEditForm>();
            //services.AddTransient<CategoryEditForm>();
            services.AddTransient<ImportExportDialog>();
            services.AddTransient<AboutDialog>();
        }
    }

    /// <summary>
    /// 对话框服务实现
    /// </summary>
    //public class DialogService : IDialogService
    //{
    //    public bool ShowConfirmation(string message, string title)
    //    {
    //        return MessageBox.Show(message, title,
    //            MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
    //    }

    //    public void ShowError(string message, string title)
    //    {
    //        MessageBox.Show(message, title,
    //            MessageBoxButtons.OK, MessageBoxIcon.Error);
    //    }
    //}

    /// <summary>
    /// 文件服务实现
    /// </summary>
    //public class FileService : IFileService
    //{
    //    public string OpenFileDialog(string filter)
    //    {
    //        using var dialog = new OpenFileDialog { Filter = filter };
    //        return dialog.ShowDialog() == DialogResult.OK ? dialog.FileName : null;
    //    }

    //    public string SaveFileDialog(string filter)
    //    {
    //        using var dialog = new SaveFileDialog { Filter = filter };
    //        return dialog.ShowDialog() == DialogResult.OK ? dialog.FileName : null;
    //    }
    //}
}